<?php
namespace Home\Controller;
use Think\Controller;
use Common\Common\HttpClient;

class TestController extends Controller {
    public function login($cookies = '') {
        if ($cookies != null) {
            $this->cookies = $cookies;
            $myHttpClient = new HttpClient ();
            $myHttpClient->setCookies ($this->cookies);

            $url = "http://pkxt.cuit.edu.cn/login.asp";
            $myHttpClient->get($url);
            $webContent = $myHttpClient->getContent ();
        }
        $jwc_cookie = $_SESSION ['jwc_cookie'];
        $myHttpClient = new HttpClient ();
        $myHttpClient->setCookies ( $jwc_cookie );
        
        $myHttpClient->get ("http://pkxt.cuit.edu.cn/login.asp");
        $header = $myHttpClient->getHeaders();//向客户端发送原始的 HTTP 报头

        $webTemp = $myHttpClient->getContent ();
        $jwc_cookie = $myHttpClient->getCookies();
         //print_r( $jwc_cookie);die();

        $postData = "user=guest&passwd=guest";
        // echo $postData;die();
        
        $myHttpClient->setUserAgent ("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36");

        $myHttpClient->post ( "http://pkxt.cuit.edu.cn/showfunction.asp", $postData );
        $this->cookies = $myHttpClient->getCookies();

        $myHttpClient->setCookies ($this->cookies);

        $myHttpClient->get ("http://pkxt.cuit.edu.cn/user-wel.asp");

        $this->cookies = $myHttpClient->getCookies();

        $myHttpClient->setCookies ($this->cookies);

        $myHttpClient->get ("http://pkxt.cuit.edu.cn/classtb.asp");

        $this->cookies = $myHttpClient->getCookies();
        $_SESSION ['jwc_cookie'] = $this->cookies;
        $webContent = $myHttpClient->getContent();

        // echo $webContent;die();

        if(empty($this->cookies)){
            throw new \Exception ( '啊偶~教务处君忙碌中，请耐心等待');
        }
        // print_r($webContent) ;
         return true;
    }
//查询课表
    public function queryClass($postData = '') {
        $this->login();
        $myHttpClient = new HttpClient ();
        $jwc_cookie = $_SESSION ['jwc_cookie'];
        $myHttpClient->setCookies ( $jwc_cookie);
        // echo $jwc_cookie;die();
        if(!$postData){
            $postData = "mode=1&depart=".urlencode(iconv('UTF-8', 'GB2312',"计算机学院"))."&pro=".urlencode(iconv('UTF-8', 'GB2312',"计算机科学本科(计工)"))."&grade=".urlencode(iconv('UTF-8', 'GB2312',"2013级"))."&class=".urlencode(iconv('UTF-8', 'GB2312',"02班"));
            // echo $postData;die();
        }
        //urlencode将字符串以 URL 编码    iconv是一个计算机程序，采用命令行界面，允许将某种特定编码的文件转换为另一种编码。

        $myHttpClient->post ( "http://pkxt.cuit.edu.cn/showclasstb.asp", $postData );
        $webContent = $myHttpClient->getContent();

        $pattern = "/<a [\s\S]*?<\/a>/";
        preg_match ( $pattern, $webContent, $a );//匹配数据

        $string = explode("\"", $a[0]);//把字符串分割为数组
        $url = $string[1];
        // print_r($string[1]);die();

        $myHttpClient->get ( "http://pkxt.cuit.edu.cn/".$url);
        $webContent = $myHttpClient->getContent();

        $pattern = "/<table [\s\S]*?<\/table>/";
        preg_match_all ( $pattern, $webContent, $table );

        $result = table_to_array ( $table [0][1] );
        // print_r($table[0][1]);
        // print_r($result);die();
        // echo $webContent;die();

        for($i = 0; $i < 5;$i++){
            $week[$i][0] = urldecode(iconv('GB2312', 'UTF-8',$result[1][$i+1]));
            $week[$i][1] = urldecode(iconv('GB2312', 'UTF-8',$result[2][$i+1]));
            $week[$i][2] = urldecode(iconv('GB2312', 'UTF-8',$result[3][$i+1]));
            $week[$i][3] = urldecode(iconv('GB2312', 'UTF-8',$result[4][$i+1]));
            $week[$i][4] = urldecode(iconv('GB2312', 'UTF-8',$result[5][$i+1]));
        }
        $this->week = $week;
        // print_r($this->week);die();
        // print_r($week);die();
        return $this->week;
        // echo $this->week;
  
    }

    public function query(){
        $data = I ( 'request.data' );
        $postData = "mode=1&depart=".urlencode(iconv('UTF-8', 'GB2312',$data["depart"]))."&pro=".urlencode(iconv('UTF-8', 'GB2312',$data["pro"]))."&grade=".urlencode(iconv('UTF-8', 'GB2312',$data["grade"]))."&class=".urlencode(iconv('UTF-8', 'GB2312',$data["class"])); 
        // echo $postData;
        $result = $this->queryClass($postData);

        // echo var_dump($result);
        $this->ajaxReturn ($result,"JSON");
        
        // echo $result;
    }
}

function table_to_array($str,$trim = false){
    if (empty($str)) {
        return false;
    }
      
    //判断是否存在table标签
    $patternTable = "/<table[\s\S]*?<\/table>/i";
    if (false == preg_match($patternTable, $str ,$table)) {
           throw new BaseException("can not find table tag");//异常处理
    }
    // print_r($table);die();
    //提取行tr
    $patternTr = "/<tr[\s\S]*?<\/tr>/i";
    preg_match_all($patternTr, $table[0], $arrTr);

    //提取行th
    $patternTh = "/<th[\s\S]*?<\/th>/i";
    preg_match_all($patternTh, $table[0], $arrTh);
    $table = array();
    foreach ($arrTh[0] as $key => $th){         //二维数组的处理
        if($trim)
          $arrTh[0][$key] = preg_replace("/&nbsp;|\s/", "", strip_tags($th,""));
        else 
            $arrTh[0][$key] = trim(strip_tags($th,""));
    }
    if (!empty($arrTh[0])) {
        array_push($table, $arrTh[0]);
    }
    // print_r($table);die();

    //提取每行每列td
    $patternTd = "/<td[\s\S]*?<\/td>/i";
    foreach ($arrTr[0] as $tr) {
        preg_match_all($patternTd, $tr, $arrTd);
        foreach ($arrTd[0] as $key => $td){
            if($trim)
                $arrTd[0][$key] = preg_replace("/&nbsp;|\s/", "", strip_tags($td,""));
            else 
                $arrTd[0][$key] = trim(strip_tags($td,""));
        }
        if (!empty($arrTd[0])) {
            array_push($table, $arrTd[0]);
        }
    }
    return $table;
}
    